#!/bin/bash  
##start all service of apex 
SERVICE_NAMES=("zookeeper" "kafka" "redis" "mysql" "postgresql-13" "nginx" "thingsboard" "thingsboard-gateway") 
DEPEND_SERVICES=("" "zookeeper" "" "" "" "" "postgresql-13" "thingsboard") 
SERVICE_KEYS=("zookeeper.server.quorum" "kafka.Kafka" "redis-server" "mysqld" "postgres: background writer" "/usr/sbin/nginx" "thingsboard.jar" "thingsboard_gateway.tb_gateway") 
APEX_HOME=/opt/apexapp/services  
nacos_home=/usr/local/nacos 
apex_apps=`ls ${APEX_HOME}` 
if [ $? -ne 0 ]; 
then 
  echo "系统没有安装亚派应用" 
  exit 1 
fi  
sorted_apps=("nantianmen" "baojinnang" "shuiliandong" "logaudit" "wuxingshan" "nanshan" "workflow" "servicecenter" "weeconfig" "mingjingtai" "uip" "wechat" "videocenter" "hvac" "td-aggregate")  
nantianmen_d= baojinnang_d=nantianmen shuiliandong_d="nantianmen baojinnang" nanshan_d=shuiliandong workflow_d=shuiliandong wuxingshan_d=shuiliandong servicecenter_d="shuiliandong" mingjingtai_d=shuiliandong uip_d=shuiliandong wechat_d=shuiliandong videocenter_d="shuiliandong nanshan" logaudit_d=shuiliandong weeconfig_d=shuiliandong hvac_d=shuiliandong td_aggregate_d=shuiliandong  
license_dir=/etc/apex/license 
jar_pwd='22HJlb4e0bacEkbP2E8nh8tgwBMrQGgJQQlB1yifFA8='  
function show_usage() 
{ 
  echo "用法:"  
  echo -e "\t./ypstart -d 0或者1 -r 0或者1 服务名" 
  echo -e "\t\t -d表示是否已后台服务的方式启动应用，允许的值为0或者1，默认为1，即以后台服务的方式启动应用" 
  echo -e "\t\t -r表示是否重启已经运行的应用，允许的值为0或者1，默认为0，即不重启" 
  echo -e "\t\t 服务名为空时，启动所有服务" 
  echo "示例:"  
  echo -e "\t以后台服务方式启动所有应用： ./ypstart" 
  echo -e "\t以后台服务方式重启所有应用： ./ypstart -r 1" 
  echo -e "\t以后台服务方式启动指定应用： ./ypstart shuiliandong" 
  echo -e "\t以后台服务方式重启指定应用： ./ypstart -r 1 shuiliandong" 
  echo -e "\t以前台应用方式启动指定应用： ./ypstart -d 0 shuiliandong" 
  echo -e "\t以前台应用方式重启指定应用： ./ypstart -d 0 -r 1 shuiliandong" 
  return 0 
}  
function license_exist() 
{ 
  files=`ls ${license_dir}/*.lic` 
  err=$? 
  if [ $err -ne 0 ]; 
  then 
    return $err 
  fi 
  return 0 
}  
function check_app(){ 
  app_pid=`ps ax | grep -i "$1" | grep java | grep -v grep | awk '{print $1}'` 
  if [ -z "${app_pid}" ] ; 
  then 
    echo "No $1 is running." return 1; 
  fi 
  echo ${app_pid} 
  return 0  
}  
function check_service(){ 
  app_pid=`ps ax | grep -i "$1" | grep -v grep | awk '{print $1}'` 
  if [ -z "${app_pid}" ] ; 
  then 
    echo "No $1 is running." return 1; 
  fi 
  return 0  
}   
function get_service_key(){ 
  for(( i=0;i<${#SERVICE_NAMES[@]};i++)) 
  do 
    if [ "$1" = "${SERVICE_NAMES[i]}" ];
    then 
      echo "${SERVICE_KEYS[i]}" 
      return 0
    fi 
  done; 
  echo "" 
  return 1
}  
function get_depend_service(){ 
  for(( i=0;i<${#SERVICE_NAMES[@]};i++)) 
  do 
    if [ "$1" = "${SERVICE_NAMES[i]}" ];
    then 
      echo "${DEPEND_SERVICES[i]}" 
      return 0 
    fi 
  done; 
  echo "" 
  return 1 
}  
function start_service() { 
  key=$(get_service_key $1) 
  if [ -z "$key" ]; 
  then  
    echo "unknown service $1" 
    return 1 
  fi 
  check_service $key 
  err=$? 
  echo "check_service $key: $err" 
  if [ $err -eq 0 ];
  then 
    echo "$1 is running" 
    return 0 
  fi 
  depend_service=$(get_depend_service $1) 
  if [ ! -z "${depend_service}" ]; 
  then 
    start_service ${depend_service} 
    err=$? 
    if [ $err -ne 0 ];
    then 
      echo "Failed to start $1 because of the failure of ${depend_service}" 
      return $err 
    fi 
  fi 
  echo "start $1 ..." 
  sudo systemctl start $1 
  echo "wait 10 seconds for its ready" 
  sleep 10 
  sudo systemctl status $1 
  err=$? 
  if [ $err -ne 0 ];
  then 
    echo "systemctl status $1: $err" 
    echo "failed to start $1" 
    return $err 
  fi 
  echo "$1 started" 
  return 0
}  
function start_nacos(){ 
  key=nacos.nacos 
  check_app $key 
  err=$? 
  echo "check_app $key: $err" 
  if [ $err -eq 0 ];
  then 
    echo "nacos is running" 
    return 0 
  fi 
  echo "start nacos ..." 
  sudo su <<EOF ?source /etc/profile 
  echo "JAVA_HOME: ${JAVA_HOME}" ?${nacos_home}/bin/startup.sh -m standalone EOF 
  echo "wait 10 seconds for its ready" 
  sleep 10 
  check_app $key err=$? 
  echo "check_app $key: $err" 
  if [ $err -ne 0 ];then 
    echo "failed to start nacos" 
    return 1 
  fi 
  echo "nacos started" 
  return 0 
}  
function start_app(){ 
  key=$1 
  r_flag=0 
  d_flag=1 
  if [ $# -ne 3 ];
  then 
    echo "invalid parameters! Need 3 paramters, not $#" 
    return 1 
  fi  
  r_flag=$2 
  d_flag=$3 
  pid=$(check_app $key) 
  err=$? 
  echo "check_app $key: $err" 
  if [ $err -eq 0 ];
  then 
    echo "$1 is already running" 
    if [ $r_flag -eq 0 ];
    then 
      return 0 
    fi 
    echo "kill running $1 (pid=$pid) and wait 3 seconds" 
    sudo kill -9 $pid 
    sleep 3 
  fi 
  echo "no $1 is running, try to start it" 
  app_name=$1 
  converted_name="${app_name/-/_}_d" 
  depended_apps=$(eval echo "\$${converted_name}") 
  if [ ! -z "${depended_apps}" ]; 
  then 
    for depended_app in ${depended_apps[@]} 
    do 
      if [ -z "${depended_app}" ]; 
      then 
        break 
      fi  
      check_app ${depended_app} 
      err=$?
      if [ $err -ne 0 ];
      then 
        echo "${depended_app} is not running, should start ${depended_app} first!" 
        return $err 
      fi 
    done 
  fi  
  conf_file=bootstrap.yml 
  app_home=${APEX_HOME}/$1 
  if [ ! -d ${app_home} ]; 
  then 
    echo "no application named $1" 
    return 1 
  fi 
  if [ ! -f "${app_home}/${conf_file}" ]; 
  then 
    conf_file=application.yml 
  fi 
  if [ ! -f "${app_home}/${conf_file}" ]; 
  then 
    echo "no config file for $1 in ${app_home}" 
    return 2 
  fi 
  cd ${app_home} 
  jar_files=(`ls -t $1*.jar`) 
  jarfile=${jar_files[0]} 
  if [ -z "$jarfile" ];
  then 
    echo "no jar file for $1 in ${app_home}" 
    return 3 
  fi 
  echo "start $1: $jarfile ${conf_file}" 
  if [ ${d_flag} -eq 0 ]; then 
    java -javaagent:${jarfile}="-pwd ${jar_pwd}" -DAppname=$1 -Xmx1024M -Xms1024M -jar ${app_home}/${jarfile} --spring.config.local=${app_home}/${conf_file} 
    return 0 
  fi 
  nohup java -javaagent:${jarfile}="-pwd ${jar_pwd}" -DAppname=$1 -Xmx1024M -Xms1024M -jar ${app_home}/${jarfile} --spring.config.local=${app_home}/${conf_file}  >/dev/null 2>&1 &      #java -DAppname=${APP_NAME} -Xmx512M -Xms512M -jar ${APP_HOME}/${APP_NAME}.jar --spring.config.local=${conf_file} 
  echo "wait 15 seconds for its ready" 
  sleep 15 
  check_app $key 
  err=$? 
  echo "check_app $key: $err" 
  if [ $err -ne 0 ];then 
    echo "failed to start $1" 
    return 3 
  fi 
  echo "$1 started" 
  return 0 
}  
function start_all_service() 
{ 
  s_apps="" 
  f_apps="" 
  for service in ${SERVICE_NAMES[@]} 
  do 
    start_service $service 
    err=$? 
    if [ $err -ne 0 ]; 
    then 
      f_apps="${f_apps}, $service" 
    else 
      s_apps="${s_apps}, $service" 
    fi 
  done 
  cur_time=$(date "+%Y-%m-%d %H:%M:%S") 
  echo "${cur_time} services successfully started : ${s_apps}" 
  echo "${cur_time} services failed to start : ${f_apps}" 
  return 0 
}  
function start_all_app() { 
  restart_flag=0 
  if [ $# -eq 1 ];
  then 
    restart_flag=$1 
  fi 
  s_apps="" 
  f_apps="" 
  for service in ${sorted_apps[@]} 
  do 
    flag=1 
    for installed_service in ${apex_apps[@]} 
    do 
      if [ "${installed_service}" == "${service}" ]; 
      then 
        flag=0 
        break
      fi 
    done 
    if [ $flag -ne 0 ]; 
    then 
      continue 
    fi 
    echo "start_app $service ${restart_flag} 1 ++++++++++++++++++++++++++++++++++++++++++++++" 
    start_app $service ${restart_flag} 1 
    err=$? 
    if [ $err -ne 0 ]; 
    then 
      f_apps="${f_apps}, $service" 
    else 
      s_apps="${s_apps}, $service" 
    fi 
  done 
  cur_time=$(date "+%Y-%m-%d %H:%M:%S") 
  echo "${cur_time} applications successfully started : ${s_apps}" 
  echo "${cur_time} applications failed to start : ${f_apps}" 
  return 0 
}   
THIS_DIR=`cd $(dirname $0); pwd` 
app=all 
daemon=1 
restart=0 
while getopts "hd:r:" opt; 
do     
  case $opt in  h)             
    show_usage             
    exit 0         
    ;;         
  d)             
    daemon=$OPTARG         
    ;; 
  r)             
    restart=$OPTARG         
    ;;       
esac 
done 
shift $(($OPTIND - 1))  
if [[ "$daemon" != "1" ]] && [[ "$daemon" != "0" ]]; 
then 
  echo "invalid option -d $daemon" 
  show_usage 
  exit 1 
fi  
if [[ "$restart" != "1" ]] && [[ "$restart" != "0" ]]; 
then 
  echo "invalid option -r $restart" 
  show_usage 
  exit 1
fi  
if [ -n "$1" ];
then     
  app=$1 
fi  
if [[ "$app" = "all" ]]; 
then 
  daemon=1 
fi  
license_exist 
err=$? 
if [ $err -ne 0 ];
then 
  echo "no license file, cannot start any application!" 
  exit 2 
fi  
start_nacos 
err=$? 
if [ $err -ne 0 ];
then 
  echo "nacos is invalid, cannot start other application!" 
  exit 1
fi  
if [ $app = "all" ];
then 
  start_all_service 
  start_all_app $restart 
  exit $? 
fi 
start_app $app $restart $daemon  exit $?  
